﻿using System.Windows.Forms;
using CSW.Framework.Common;
using CSW.Framework.Common.Exceptions;

namespace CSW.Framework.UI.DevEx.Forms
{
    /// <summary>
    /// Progress form.  See <see cref="AsyncProcessDelegate{ParameterType,ResultType}" />.
    /// </summary>
    public static class DXProgressForm<T1, TResult>
    {
        /// <summary>
        /// Runs the specified asynchronous process delegate.
        /// </summary>
        /// <param name="asyncProcessDelegate">The async process delegate.</param>
        /// <param name="arg1">The first argument.</param>
        /// <returns>The result.</returns>
        public static TResult Run(AsyncProcessDelegate<T1, TResult> asyncProcessDelegate, T1 arg1)
        {
            return Run(asyncProcessDelegate, arg1, ExceptionHelper.ShowException, null);
        }

        /// <summary>
        /// Runs the specified asynchronous process delegate.
        /// </summary>
        /// <param name="asyncProcessDelegate">The async process delegate.</param>
        /// <param name="arg1">The first argument.</param>
        /// <param name="owner">Shows the progress form with the specified owner.</param>
        /// <returns>The result.</returns>
        public static TResult Run(AsyncProcessDelegate<T1, TResult> asyncProcessDelegate, T1 arg1, IWin32Window owner)
        {
            return Run(asyncProcessDelegate, arg1, ExceptionHelper.ShowException, owner);
        }

        /// <summary>
        /// Runs the specified asynchronous process delegate.
        /// </summary>
        /// <param name="asyncProcessDelegate">The async process delegate.</param>
        /// <param name="arg1">The first argument.</param>
        /// <param name="exceptionHandler">The exception handler.</param>
        /// <returns>The result.</returns>
        public static TResult Run(AsyncProcessDelegate<T1, TResult> asyncProcessDelegate, T1 arg1, ExceptionHandlerDelegate exceptionHandler)
        {
            return Run(asyncProcessDelegate, arg1, exceptionHandler, null);
        }

        /// <summary>
        /// Runs the specified asynchronous process delegate.
        /// </summary>
        /// <param name="asyncProcessDelegate">The async process delegate.</param>
        /// <param name="arg1">The first argument.</param>
        /// <param name="exceptionHandler">The exception handler.</param>
        /// <param name="owner">Shows the progress form with the specified owner.</param>
        /// <returns>The result.</returns>
        public static TResult Run(AsyncProcessDelegate<T1, TResult> asyncProcessDelegate, T1 arg1, ExceptionHandlerDelegate exceptionHandler, IWin32Window owner)
        {
            return DXProgressForm<T1, AsyncProcessDelegate<T1, TResult>, TResult>
                .Run(Run2, arg1, asyncProcessDelegate, exceptionHandler, owner);
        }

        private static TResult Run2(AsyncProcessHelper helper, T1 arg1, AsyncProcessDelegate<T1, TResult> method)
        {
            return method(helper, arg1);
        }
    }
}